package com.ddwl.goods.service;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.goods.dao.AcademyCategoryDao;
import com.ddwl.goods.dao.model.AcademyCategory;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.AcademyCategoryBo;
import com.ddwl.schema.bo.goods.QueryAcademyCategoryBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.goods.AcademyCategoryVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;

/**
 * 商学院分类service
 * @author wenbin
 */
@Service
public class AcademyCategoryService {


    @Resource
    private AcademyCategoryDao academyCategoryDao;

    /**
     * 查询列表
     * @param boPageBo
     * @return
     */
    public PageVo<AcademyCategoryVo> academyCategoryList(PageBo<QueryAcademyCategoryBo> boPageBo) {
        PageHelper.startPage(boPageBo.getPage(),boPageBo.getPageSize(), MySqlUtil.orderBy(boPageBo.getSort(),boPageBo.isAsc()));
        PageInfo<AcademyCategory> info = new PageInfo(academyCategoryDao.queryAcademyCategory(boPageBo.getParam()));
        List<AcademyCategoryVo> list = BeanConverUtils.convertBeanList(info.getList(),AcademyCategoryVo.class);
        return new PageVo<>(info.getTotal(),boPageBo.getPage(),boPageBo.getPageSize(),list);
    }

    /**
     * 创建文章类型
     * @param bo
     * @return
     */
    @Transactional(rollbackFor = Throwable.class)
    public void createAcademyCategory(AcademyCategoryBo bo, String userId) {
        ParamException.isNotNull(bo.getId(),"分类id不为空");
        ParamException.isBlank(bo.getAcademyCategoryName(),"分类名称为空");

        AcademyCategory entity = BeanConverUtils.convertBean(bo, AcademyCategory.class);

        //分类名称不能重复
        AcademyCategory academyCategory = new AcademyCategory();
        academyCategory.setAcademyCategoryName(entity.getAcademyCategoryName());
        AcademyCategory category = academyCategoryDao.selectOne(academyCategory);
        ParamException.isNotNull(category,"分类名称重复");

        entity.setEffect(0);
        entity.setCreatedUserId(userId);
        academyCategoryDao.insert(entity);
    }

    /**
     * 修改文章类型
     * @param bo
     * @return
     */
    @Transactional(rollbackFor = Throwable.class)
    public void updateAcademyCategory(AcademyCategoryBo bo,String userId) {
        ParamException.isBlank(bo.getId(),"分类id为空");

        AcademyCategory entity = BeanConverUtils.convertBean(bo, AcademyCategory.class);
        entity.setModifiedUserId(userId);

        academyCategoryDao.updateByPrimaryKeySelective(entity);

        //分类名称不能重复
        if (!StringUtils.hasText(entity.getAcademyCategoryName())){
            AcademyCategory academyCategory = new AcademyCategory();
            academyCategory.setAcademyCategoryName(entity.getAcademyCategoryName());
            List<AcademyCategory> academyCategoryList = academyCategoryDao.select(academyCategory);
            Boolean check= academyCategoryList!=null&&academyCategoryList.size()>1;
            ParamException.isTrue(check,"分类名称重复");
        }
    }

    /**
     * 删除文章类型
     * @param id
     * @param userId
     * @return
     */
    public void deleteAcademyCategory(String id,String userId) {
        ParamException.isBlank(id,"分类id为空");
        AcademyCategory entity = new AcademyCategory();
        entity.setId(id);
        entity.setGmtModified(Instant.now());
        entity.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        entity.setModifiedUserId(userId);
        academyCategoryDao.updateByPrimaryKeySelective(entity);
    }

    public List<AcademyCategoryVo> getList() {
        return BeanConverUtils.convertBeanList(academyCategoryDao.getList(),AcademyCategoryVo.class);
    }
}

